$(function() {
	optErrMsg();
	doParticleground();
	showLoginForm();
});

$("#errMsgContiner").hide();
function optErrMsg() {
	$("#showErrMsg").html('');
	$("#errMsgContiner").hide();
}

// 输入验证码，回车登录
$(document).keydown(function(e) {
	if (e.keyCode == 13) {
		$("#but_login").click();
	}
});

// 验证用户信息
function checkUser() {
	if (!validForm()) {
		return false;
	}
	newLogin();
}
// 表单验证
function validForm() {
	if ($.trim($("#userName").val()).length == 0) {
		showErrorMsg("请输入用户名");
		$('#userName').animateCss('shake', function() {
			$('#userName').focus();
		});
		return false;
	}

	if ($.trim($("#password").val()).length == 0) {
		showErrorMsg("请输入密码");
		$('#password').animateCss('shake', function() {
			$('#password').focus();
		});
		return false;
	}

	// if ($.trim($("#randCode").val()).length == 0) {
	// showErrorMsg("请输入验证码");
	// return false;
	// }
	return true;
}

// 登录处理函数
function newLogin(orgId) {
	setCookie();
	var actionurl = $('form').attr('action');// 提交路径
	var checkurl = $('form').attr('check');// 验证路径
	var formData = new Object();
	var data = $(":input").each(function() {
		formData[this.name] = $("#" + this.name).val();
	});
	formData['orgId'] = orgId ? orgId : "";
	// 语言
	formData['langCode'] = $("#langCode").val();
	formData['langCode'] = $("#langCode option:selected").val();

	var loading = layer.load();
	$.ajax({
		async : true,
		cache : false,
		type : 'POST',
		url : checkurl,// 请求的action路径
		data : formData,
		error : function() {// 请求失败处理函数
			layer.close(loading);
		},
		success : function(data) {
			layer.close(loading);
			var d = $.parseJSON(data);
			if (!d.success) {
				$('#password').val('');
				showErrorMsg(d.msg);
			} else {
				if (!(d.attributes.orgNum > 1)) {
					showMainFrame(actionurl);

					// $('#content').animateCss('bounceOut', function() {
					// window.location.href = actionurl;
					// });
				} else {
					// 用户拥有多个部门，需选择部门进行登录
					var title, okButton;
					if ($("#langCode").val() == 'en') {
						title = "Please select Org";
						okButton = "Ok";
					} else {
						title = "请选择组织机构";
						okButton = "确定";
					}
					$.dialog({
						id : 'LHG1976D',
						title : title,
						max : false,
						min : false,
						drag : false,
						resize : false,
						content : 'url:userController.do?userOrgSelect&userId=' + d.attributes.user.id,
						lock : true,
						button : [ {
							name : okButton,
							focus : true,
							callback : function() {
								iframe = this.iframe.contentWindow;
								var orgId = $('#orgId', iframe.document).val();
								formData['orgId'] = orgId ? orgId : "";
								$.ajax({
									async : false,
									cache : false,
									type : 'POST',
									url : 'loginController.do?changeDefaultOrg',// 请求的action路径
									data : formData,
									error : function() {// 请求失败处理函数
									},
									success : function(data) {
										// window.location.href = actionurl;
										showMainFrame(actionurl);
									}
								});
								this.close();
								return false;
							}
						} ],
						close : function() {
							showMainFrame(actionurl);
							// setTimeout("window.location.href='" + actionurl + "'", 10);
						}
					});
				}
			}
		}
	});
}
// 登录提示消息显示
function showErrorMsg(msg) {
	$("#errMsgContiner").show();
	$("#showErrMsg").html(msg);
	window.setTimeout(optErrMsg, 3000);
}
// 设置cookie
function setCookie() {
	if ($('#on_off').val() == '1') {
		$("input[iscookie='true']").each(function() {
			$.cookie(this.name, $("#" + this.name).val(), "/", 24);
			$.cookie("COOKIE_NAME", "true", "/", 24);
		});
	} else {
		$("input[iscookie='true']").each(function() {
			$.cookie(this.name, null);
			$.cookie("COOKIE_NAME", null);
		});
	}
}
// 读取cookie
function getCookie() {
	var COOKIE_NAME = $.cookie("COOKIE_NAME");
	if (COOKIE_NAME != null) {
		$("input[iscookie='true']").each(function() {
			$($("#" + this.name).val($.cookie(this.name)));
			if ("admin" == $.cookie(this.name)) {
				$("#randCode").focus();
			} else {
				$("#password").val("");
				$("#password").focus();
			}
		});
		$("#on_off").attr("checked", true);
		$("#on_off").val("1");
	} else {
		$("#on_off").attr("checked", false);
		$("#on_off").val("0");
		$("#randCode").focus();
	}
}

function doParticleground() {
	$('#particles').particleground({
		minSpeedX : 0.1,
		maxSpeedX : 0.3,
		minSpeedY : 0.1,
		maxSpeedY : 0.3,
		directionX : 'center', // 'center', 'left' or 'right'. 'center' = dots
		// bounce off edges
		directionY : 'center', // 'center', 'up' or 'down'. 'center' = dots bounce
		// off edges
		density : 20000, // How many particles will be generated: one particle
		// every n pixels
		dotColor : '#D6D6D6',
		lineColor : '#D6D6D6',
		particleRadius : 5, // Dot size
		lineWidth : 1,
		// curvedLines: true,
		proximity : 120, // How close two dots need to be before they join
		parallax : false
	});
}

function showLoginForm() {
	// $('#content').show();
	$('#content').animateCss('zoomInDown', function() {
		$('#userName').focus();
	});
}

function showMainFrame(url) {
	$('#content').animateCss('zoomOutDown', function() {
		window.location.href = url;
		return true;
	});
}